package de.grosjohann.android.feuerwehr.adapter;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.text.format.DateUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import de.grosjohann.android.feuerwehr.R;
import de.grosjohann.android.feuerwehr.domain.CalendarItem;

public class CalendarListAdapter
  extends BaseAdapter {

  private static final String NAME = "CalendarListAdapter";

  private static final List<CalendarItem> list = new ArrayList<CalendarItem>();

  private final Activity context;
  private final int listViewId;
  private final boolean attachToRoot;

  public CalendarListAdapter(Activity ctx, int viewId) {
    this(ctx, viewId, null, false);
  }

  public CalendarListAdapter(Activity ctx, int viewId, List<CalendarItem> newList) {
    this(ctx, viewId, newList, false);
  }

  public CalendarListAdapter(Activity ctx, int viewId, List<CalendarItem> newList, boolean attachViewToRoot) {
    context = ctx;
    listViewId = viewId;
    list.clear();
    if (newList != null) {
      list.addAll(newList);
    }
    attachToRoot = attachViewToRoot;
  }

  @Override
  public int getCount() {
    return list.size();
  }

  @Override
  public CalendarItem getItem(int i) {
    return list.get(i);
  }

  @Override
  public long getItemId(int i) {
    return i;
  }

  @Override
  public boolean isEnabled(int i) {
    return false;
  }

  @Override
  public boolean areAllItemsEnabled() {
    return false;
  }

  @Override
  public View getView(int index, View convertView, ViewGroup parent) {
    View row = null;
    CalendarItem item = list.get(index);

    if (convertView == null) {
      row = LayoutInflater.from(context).inflate(listViewId, parent, attachToRoot);
    } else {
      row = convertView;
    }

    TextView tvDate = (TextView) row.findViewById(R.id.tvDate);
    tvDate.setText(formatPeriod(context, item.getStartDate(), item.getEndDate()));
    TextView tvHeadline = (TextView) row.findViewById(R.id.tvHeadline);
    tvHeadline.setText(item.getHeadline());

    return row;
  }

  public void addAll(List<CalendarItem> newList) {
    list.clear();
    list.addAll(newList);

    Log.d(NAME, list.toString());

    super.notifyDataSetChanged();
  }

  public static String formatPeriod(Context ctx, Date startDate, Date endDate) {

    /*
     * Heute
     */
    if (DateUtils.isToday(startDate.getTime())) {
      if (startDate.getTime() <= System.currentTimeMillis() && endDate.getTime() >= System.currentTimeMillis()) {
        return (String) ctx.getText(R.string.lblNow);
      }

      if (startDate.equals(endDate)) {
        return ctx.getText(R.string.lblToday) + ", " + ctx.getText(R.string.lblFullDay);
      }

      SimpleDateFormat fmt = new SimpleDateFormat("HH:mm");
      return ctx.getText(R.string.lblToday) + ", " + fmt.format(startDate) + " - " + fmt.format(endDate);
    }

    /*
     * Morgen
     */
    if (isTomorrow(startDate)) {
      if (startDate.equals(endDate)) {
        return ctx.getText(R.string.lblTomorrow) + ", " + ctx.getText(R.string.lblFullDay);
      }

      SimpleDateFormat fmt = new SimpleDateFormat("HH:mm");
      return ctx.getText(R.string.lblTomorrow) + ", " + fmt.format(startDate) + " - " + fmt.format(endDate);
    }

    /*
     * irgend wann
     */
    if (startDate.equals(endDate)) {
      return new SimpleDateFormat("dd.MM.yyyy").format(startDate) + ", " + ctx.getText(R.string.lblFullDay);
    }

    SimpleDateFormat fmtHourMin = new SimpleDateFormat("HHmm");
    if (Integer.parseInt(fmtHourMin.format(startDate)) == 0 && Integer.parseInt(fmtHourMin.format(endDate)) == 0) {
      SimpleDateFormat fmtDate = new SimpleDateFormat("dd.MM.yyyy");
      return fmtDate.format(startDate) + " - " + fmtDate.format(endDate);
    }

    return new SimpleDateFormat("dd.MM.yyyy, HH:mm - ").format(startDate)
        + new SimpleDateFormat("HH:mm").format(endDate);
  }

  private static boolean isTomorrow(Date date) {
    SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMdd");
    Calendar cal = Calendar.getInstance();
    cal.setTime(new Date(System.currentTimeMillis()));
    cal.add(Calendar.DATE, 1);

    return Integer.parseInt(fmt.format(date)) == Integer.parseInt(fmt.format(cal.getTime()));
  }
}
